<template>
  <section>
    <div class="model retrieval">
      <img :src="IMGBASE64.JIANSUOGUANLI2X" class="orange">
    	<span class="title">检索管理</span>
    	<div class="body">
    		<span>年级：</span>
    		<select class="select" v-model="gradeSelect" :style="{background: `url(${IMGBASE64.SELECT}) no-repeat scroll right center`}">
    			<option v-for="(item, key) in gradeOption" :label="item" :value="key"></option>
    		</select>
    		<el-button type="primary" @click="getNumData()" size="small">确认</el-button>
        <el-button type="primary" @click="clearGradeData()" size="small">重置</el-button>
    		<div class="right">
    			<a :href="`${this._httpUrl}studentInfo/numberQuery/export?yxt-xtoken=${exportUrl}`">
            <el-button type="primary" size="small">导出</el-button>
          </a>
    		</div>
    	</div>
    </div>

    <div class="model query">
      <img :src="IMGBASE64.XUESHENGRENSHU2X" class="pink">
    	<span class="title">学生人数查询</span>
    	<div class="kong"></div>
    	<template>
    		<el-table :data="formData" style="width: 100%">
    			<el-table-column prop="grade" label="年级名称" align="center"></el-table-column>
    			<el-table-column prop="clazz" label="班号" align="center"></el-table-column>
    			<el-table-column prop="num" label="人数" align="center"></el-table-column>
    			<el-table-column prop="classTeacherName" label="班主任" align="center"></el-table-column>
    		</el-table>
    	</template>
      <el-pagination
        v-show="paging"
        @current-change="handleCurrentChange"
        :page-size="20"
        layout="prev, pager, next, jumper"
        :total="total"
        class="pull-right">
      </el-pagination>
    </div>
  </section>
</template>

<script>
export default {
  data () {
    return {
      exportUrl: null,
      formData: [],
      gradeSelect: null,
      gradeOption: [],
      total: 0,
      page: 1,
      paging: true,
      allNameData: [],
      tablemsg: []
    }
  },
  mounted () {
    this.getGradeData()
    this.getNumData()
    this.exportUrl = this._exportUrl()
  },
  methods: {
    _exportUrl () {
      let _cookie = document.cookie.split(';')
      let _cookieArray = []
      _cookie.forEach(v => {
        let _d = v.split('=')
        _cookieArray.push({
          key: _d[0],
          value: _d[1]
        })
      })
      return _cookieArray[0].value
    },
    handleCurrentChange (val) {
      this.page = val
      this.getNumData()
    },
    getNumData () {
      this.$http.post(this._httpUrl + 'studentInfo/numberQuery', {page: (this.page), rows: 20, grade: this.gradeSelect}, {'headers': {'yxt-xtoken': localStorage.xtoken}}).then(function (response) {
        if (response.body.code === '000') {
          this.formData = response.data.data.rows
          this.total = response.data.data.total
          this.getAllData()
          this.paging = true
          for (var i = 0; i < this.formData.length; i++) {
            this.formData[i].numValue = i + 1
            for (var j in this.gradeOption) {
              if (this.formData[i].grade === parseInt(j)) {
                this.formData[i].grade = this.gradeOption[j]
              }
            }
          }
          if (this.gradeSelect !== null && this.formData !== []) {
            this.formData.unshift({numValue: 0, grade: '人数合计', clazz: '--', num: null, classTeacherName: '--'})
            this.paging = false
            let n = 0
            for (var x = 0; x < this.formData.length; x++) {
              if (x === 0) {
                continue
              } else {
                n += this.formData[x].num
              }
            }
            this.formData[0].num = n
          }
        } else if (response.body.code === '887') {
          this.$router.push('/')
        } else if (response.body.code === '889') {
          this.$router.push('/')
        }
      })
    },
    getAllData () {
      this.$http.post(this._httpUrl + 'studentInfo/numberQuery', {page: (this.page), rows: this.total}, {'headers': {'yxt-xtoken': localStorage.xtoken}}).then(function (response) {
        if (response.body.code === '000') {
          this.allNameData = response.data.data.rows
        } else if (response.body.code === '887') {
          this.$router.push('/')
        } else if (response.body.code === '889') {
          this.$router.push('/')
        }
      })
    },
    // getGradeDatas () {
    //   if (this.gradeSelect !== null) {
    //     this.formData = []
    //     this.paging = false
    //     var z = 0
    //     for (var i = 0; i < this.allNameData.length; i++) {
    //       if (this.allNameData[i].grade === parseInt(this.gradeSelect)) {
    //         for (var x in this.gradeOption) {
    //           if (this.allNameData[i].grade === parseInt(x)) {
    //             this.allNameData[i].grade = this.gradeOption[x]
    //           }
    //         }
    //         this.formData.push(this.allNameData[i])
    //         this.formData[z].numValue = ++z
    //       }
    //     }
    //   }
    //   if (this.gradeSelect === null) {
    //     this.getNumData()
    //     this.paging = true
    //   }
    // },
    getGradeData () {
      this.$http.post(this._httpUrl + 'public/grade/query', {}, {'headers': {'yxt-xtoken': localStorage.xtoken}}).then(function (response) {
        if (response.body.code === '000') {
          this.gradeOption = response.data.data.gradeMap
        } else if (response.body.code === '887') {
          this.$router.push('/')
        } else if (response.body.code === '889') {
          this.$router.push('/')
        }
      })
    },
    clearGradeData () {
      this.gradeSelect = null
    }
  }
}
</script>

<style lang="less" scoped>
@import '/static/css/all-use.css';
	.retrieval{
	  .body{
	    span{
	    	margin-left: 4%;
	    }
	    span:first-child{
	    	margin-left: 0;
	    }
	    .right{
	    	float: right;
	    	margin-right: 5%;
	    }
	  }
	  .select{
	    width: 10%;
	    border: 0;
	    border-bottom: 1px solid #D7D7D7;
	  }
	}

</style>
